package com.tomtaw.eworldtomtawclouddoctorsdkdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.tomtaw.biz_login.ECloudSDK;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.login_name_ed)
    EditText loginNameEd;
    @BindView(R.id.login_pw_ed)
    EditText loginPwEd;
    @BindView(R.id.ip_address_ed)
    EditText ipAddressEd;
    @BindView(R.id.token_ed)
    EditText tokenEd;
    @BindView(R.id.free_login_chk)
    CheckBox mFreeLoginChk;

    Unbinder mUnbinder;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mUnbinder = ButterKnife.bind(this);
    }


    @OnClick(R.id.free_login_chk)
    void onClickFreeLoginChk(View view){
        boolean bSel = mFreeLoginChk.isSelected();
        tokenEd.setVisibility(!bSel?View.VISIBLE:View.GONE);
        loginNameEd.setVisibility(!bSel?View.GONE:View.VISIBLE);
        loginPwEd.setVisibility(!bSel?View.GONE:View.VISIBLE);
        mFreeLoginChk.setSelected(!bSel);
    }

    //用用户名和密码登录
    @OnClick(R.id.login_btn)
    void onClickLogin(View view) {
        if (mFreeLoginChk.isSelected()){
            String token = tokenEd.getText().toString();

            ECloudSDK.getInstance().login(this,token, new ECloudSDK.OnLoginListener() {
                @Override
                public void onLoginSuccess() {

                }

                @Override
                public void onLoginFailure(String msg) {
                    Toast.makeText(MainActivity.this,msg,Toast.LENGTH_LONG).show();
                }
            });
        }else {
            String name = loginNameEd.getText().toString();
            String pw = loginPwEd.getText().toString();

            ECloudSDK.getInstance().login(this, name, pw, new ECloudSDK.OnLoginListener() {
                @Override
                public void onLoginSuccess() {

                }

                @Override
                public void onLoginFailure(String msg) {
                    Toast.makeText(MainActivity.this,msg,Toast.LENGTH_LONG).show();
            }
            });
        }


    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
    }
}
